//
//  NSDate+Extension.m
//  client_confirm
//
//  Created by fdh on 14-7-1.
//  Copyright (c) 2014年 fdh. All rights reserved.
//


#import "NSDate+extension.h"

@implementation NSDate(extension)

+(long) gk_now;
{
    return (long)[[NSDate date] timeIntervalSince1970];
}

+(long long) gk_now64;
{
    return (long long)([[NSDate date] timeIntervalSince1970] * 1000);
}

static NSDateFormatter *DefaultFormatterYMDHMSSS = nil;//YYYY-MM-dd HH:mm:ss.SSS
static NSDateFormatter *DefaultFormatterYMDHMS = nil;//YYYY-MM-dd HH:mm:ss
static NSDateFormatter *DefaultFormatterYMDHM = nil;//YYYY-MM-dd HH:mm
static NSDateFormatter *DefaultFormatterYMD = nil;//YYYY-MM-dd
static NSDateFormatter *DefaultFormatterMDHM = nil;//MM-dd HH:mm
static NSDateFormatter *DefaultFormatterMD = nil;//MM/dd
static NSDateFormatter *DefaultFormatterM_D = nil;//MM-dd
static NSDateFormatter *DefaultFormatterHM = nil;//HH:mm

+ (NSDateFormatter *)formatterWithFormat:(NSString *)format{
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:format];
    return formatter;
}

+ (void)initialize{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        DefaultFormatterYMDHMSSS = [self formatterWithFormat:@"YYYY-MM-dd HH:mm:ss.SSS"];
        DefaultFormatterYMDHMS = [self formatterWithFormat:@"YYYY-MM-dd HH:mm:ss"];
        DefaultFormatterYMDHM = [self formatterWithFormat:@"YYYY-MM-dd HH:mm"];
        DefaultFormatterYMD = [self formatterWithFormat:@"YYYY-MM-dd"];
        DefaultFormatterMDHM = [self formatterWithFormat:@"MM-dd HH:mm"];
        DefaultFormatterMD = [self formatterWithFormat:@"MM/dd"];
        DefaultFormatterM_D = [self formatterWithFormat:@"MM-dd"];
        DefaultFormatterHM = [self formatterWithFormat:@"HH:mm"];
    });
}

- (NSString *)FORMAT_YMDHMS
{
    return [DefaultFormatterYMDHMS stringFromDate:self];
}

- (NSString *)FORMAT_YMD
{
    NSString *newTimeStr=[DefaultFormatterYMD stringFromDate:self];
    return newTimeStr;
}

+ (NSString *)fullTimeString{
    NSString *newTimeStr=[DefaultFormatterYMDHMSSS stringFromDate:[NSDate date]];
    return newTimeStr;
}

+ (NSString *)custom_YMDHM:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterYMDHM stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)custom_MDHM:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterMDHM stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)custom_MD:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterM_D stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)custom_YMDHMS:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterYMDHMS stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)custom_YMD:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterYMD stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)recentlyTimeStringWithTimeline:(long long)timeline{
    if (timeline > 144913545800) {
        timeline /= 1000;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeline];
    return [date recentlyTimeString];
}

- (NSString *)recentlyTimeString{
    
    NSDate *date = self;
    NSDate *now = [NSDate new];
    NSTimeInterval delta = now.timeIntervalSince1970 - date.timeIntervalSince1970;
    if (delta < -60 * 10) { // 本地时间有问题
        return [DefaultFormatterYMD stringFromDate:date];
    } else if (delta < 60) { // 1分钟内
        return @"刚刚";
    } else if (delta < 60 * 60) { // 1小时内
        return [NSString stringWithFormat:@"%d分钟前", (int)(delta / 60.0)];
    } else if (date.isToday) {
        return [NSString stringWithFormat:@"%d小时前", (int)(delta / 60.0 / 60.0)];
    } else {
        return [DefaultFormatterM_D stringFromDate:date];
    }
    /*
    if (date.isYesterday) {
        return [formatterYesterday stringFromDate:date];
    } else if (date.year == now.year) {
        return [formatterSameYear stringFromDate:date];
    } else {
        return [formatterFullDate stringFromDate:date];
    }// */
}
+ (NSString *)listTimeStringWithTimeline:(long long)timeline{
    
    if (timeline > 144913545800) {
        timeline /= 1000;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeline];
    return [date listTimeString];
}
- (NSString *)listTimeString{
    NSDate *date = self;
    if (date.isToday) {
        return [DefaultFormatterHM stringFromDate:date];
    }
    return [DefaultFormatterM_D stringFromDate:date];
}

+ (NSString *)detailTimeStringWithTimeline:(long long)timeline{
    
    if (timeline > 144913545800) {
        timeline /= 1000;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeline];
    return [date detailTimeString];
}
- (NSString *)detailTimeString{
    NSDate *date = self;
    if (date.isToday) {
        return [DefaultFormatterHM stringFromDate:date];
    }
    return [DefaultFormatterMDHM stringFromDate:date];
}
@end
